package cn.ming.wfchat.app.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.ViewDataBinding;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.me.activity.RedPageListActivity;
import cn.ming.wfchat.app.utils.ImageLoaderUtil;
import cn.ming.wfchat.databinding.DialogRedPageBinding;
import cn.ming.wfchat.kit.dialog.CenterDialog;
import cn.ming.wfchat.kit.net.base.StatusResult;
import cn.ming.wfchat.kit.utils.Tools;
import cn.wildfirechat.message.SendRedPageMessageContent;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2020-05-09 22:24
 * 付款弹框
 */
public class RedPageDialog extends CenterDialog {
    private DialogRedPageBinding binding;


    @Override
    public int getLayoutId() {
        return R.layout.dialog_red_page;
    }

    public static RedPageDialog newInstance(SendRedPageMessageContent content, long messageId) {
        RedPageDialog dialog = new RedPageDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelable("content", content);
        bundle.putLong("messageId", messageId);
        dialog.setArguments(bundle);
        return dialog;
    }


    @Override
    public void initView(ViewDataBinding dataBinding) {

        binding = (DialogRedPageBinding) dataBinding;
        SendRedPageMessageContent content = getArguments().getParcelable("content");
        binding.tvTitle.setText(content.getSlogan());
        UserInfo info = ChatManager.Instance().getUserInfo(content.getFromUserId(), false);
        binding.tvName.setText(ChatManager.Instance().getUserDisplayName(info) + "的红包");
        ImageLoaderUtil.display(binding.ivImg, info.portrait);
        binding.tvDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), RedPageListActivity.class);
                intent.putExtra("hash", content.getHash());
                getActivity().startActivity(intent);
            }
        });
        binding.tvKai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppService.Instance().grapRedpage(content.getHash(), new AppService.SendRedPageCallback() {
                    @Override
                    public void onUiSuccess(StatusResult result) {
                        dismiss();
                        content.setIsstate(1);
                        ChatManager.Instance().updateMessage(getArguments().getLong("messageId"), content);
                        Intent intent = new Intent(getActivity(), RedPageListActivity.class);
                        intent.putExtra("hash", content.getHash());
                        getActivity().startActivity(intent);
                    }

                    @Override
                    public void onUiFailure(int code, String msg) {
                        if (code == 3) {
                            binding.tvKai.setVisibility(View.INVISIBLE);
                            binding.tvTitle.setText("手慢了，红包派完了");
                            content.setIsstate(1);
                            ChatManager.Instance().updateMessage(getArguments().getLong("messageId"), content);
                            binding.tvDetails.setVisibility(View.VISIBLE);
                            binding.tvDetails.setText("看看大家的手气>");
                        } else {
                            Tools.showToast(getActivity(), msg);
                        }

                    }
                });
            }
        });
        if (content.getFromUserId().equals(ChatManager.Instance().getUserId())) {
            binding.tvDetails.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public boolean isCancele() {
        return true;
    }


}
